package me.alide.modules.wechart.cp.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import me.alide.common.utils.R;
import me.alide.modules.wechart.cp.config.WxCpConfiguration;
import me.alide.modules.wechart.cp.config.WxCpProperties;
import me.alide.modules.wechart.cp.service.GovPeopleService;
import me.alide.modules.wechart.cp.service.impl.GovPeopleServiceImpl;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static me.alide.common.utils.Constant.DATA;

@RestController
@RequestMapping("wx/app/")
public class WxAppController {
/*
    @Autowired
    WxCpProperties wxCpProperties;


    @GetMapping("tags")
    public R tags() throws WxErrorException {
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId());
        List<WxCpTag> tags = wxCpService.getTagService().listAll();
        return R.ok().put(DATA, tags);
    }


    @GetMapping("users")
    public R users() throws WxErrorException {
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId());
        List<WxCpUser> users = wxCpService.getTagService().listUsersByTagId("2");
        return R.ok().put(DATA, users.size());
    }

    GovPeopleService  govPeopleService;

    @GetMapping("patrol_grids_area_count")
    public R patrolGrids() throws WxErrorException {
        if(govPeopleService == null) {
            govPeopleService = new GovPeopleServiceImpl(WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId()));
        }
        List<WxGrid> grids = govPeopleService.patrolListGrid();
        Map<String, Integer>  gridAreaCounts = new HashMap<>();
        grids.forEach(grid -> {
            String area = null;
            if(grid.getGrid_name().contains("街道")) {
                area = grid.getGrid_name().split("街道")[0];
            }
            else if(grid.getGrid_name().contains("镇")) {
                area = grid.getGrid_name().split("镇")[0];
            }
           else if(grid.getGrid_name().contains("乡")) {
                area = grid.getGrid_name().split("乡")[0];
            }
           else {
                area = grid.getGrid_name();
            }
            if(gridAreaCounts.containsKey(area)){
                gridAreaCounts.put(area, gridAreaCounts.get(area) + 1);
            }else {
                gridAreaCounts.put(area, 1);
            }
        });
        return R.ok().put(DATA, gridAreaCounts);
    }


    @GetMapping("resident_grids_area_count")
    public R residentGrids() throws WxErrorException {
        if(govPeopleService == null) {
            govPeopleService = new GovPeopleServiceImpl(WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId()));
        }
        List<WxGrid> grids = govPeopleService.residentListGrid();
        Map<String, Integer>  gridAreaCounts = new HashMap<>();
        grids.forEach(grid -> {
            String area = null;
            if(grid.getGrid_name().contains("街道")) {
                area = grid.getGrid_name().split("街道")[0];
            }
            else if(grid.getGrid_name().contains("镇")) {
                area = grid.getGrid_name().split("镇")[0];
            }
            else if(grid.getGrid_name().contains("乡")) {
                area = grid.getGrid_name().split("乡")[0];
            }
            else {
                area = grid.getGrid_name();
            }
            if(gridAreaCounts.containsKey(area)){
                gridAreaCounts.put(area, gridAreaCounts.get(area) + 1);
            }else {
                gridAreaCounts.put(area, 1);
            }
        });
        return R.ok().put(DATA, gridAreaCounts);
    }

    @GetMapping("patrol_report_list")
    public R patrolReportList() throws WxErrorException {
        if(govPeopleService == null) {
            govPeopleService = new GovPeopleServiceImpl(WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId()));
        }
        JSONArray jsonObject = govPeopleService.listPatrolReportList();
        return R.ok().put(DATA, jsonObject);
    }


    @GetMapping("resident_report_list")
    public R reportList() throws WxErrorException {
        if(govPeopleService == null) {
            govPeopleService = new GovPeopleServiceImpl(WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId()));
        }
        JSONArray jsonObject = govPeopleService.listResidentReportList();
        return R.ok().put(DATA, jsonObject);
    }

    @GetMapping("report/patrol/get_corp_status")
    public R  patrolCorpStatus() throws  WxErrorException {
        if(govPeopleService == null) {
            govPeopleService = new GovPeopleServiceImpl(WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId()));
        }
        return R.ok().put(DATA, govPeopleService.patrolCorpStatus());
    }



    @GetMapping("report/patrol/category_statistic")
    public R  patrolCategoryStatistic () throws  WxErrorException{
        if(govPeopleService == null) {
            govPeopleService = new GovPeopleServiceImpl(WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId()));
        }
        return R.ok().put(DATA, govPeopleService.patrolCategoryStatistic());
    }

    @GetMapping("report/resident/get_corp_status")
    public R  residentCorpStatus() throws  WxErrorException{
        if(govPeopleService == null) {
            govPeopleService = new GovPeopleServiceImpl(WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId()));
        }
        return R.ok().put(DATA, govPeopleService.residentCorpStatus());
    }


    @GetMapping("report/resident/category_statistic")
    public R  residentCategoryStatistic() throws  WxErrorException{
        if(govPeopleService == null) {
            govPeopleService = new GovPeopleServiceImpl(WxCpConfiguration.getCpService(wxCpProperties.getAppConfigs().get(0).getAgentId()));
        }
        return R.ok().put(DATA, govPeopleService.residentCategoryStatistic());
    }*/


}
